from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import json
import sys

class LoginHandler(BaseHTTPRequestHandler):
    def __init__(self, *args, **kwargs):
        print("LoginHandler Init")
        super().__init__(*args, **kwargs)

    def do_GET(self):
        parse_uri = urlparse(self.path)
        #params = parse_qs(parse_uri.query)
        if parse_uri.path == "/risk_check":
            print("risk_check:", parse_uri.query)
            data = json.dumps({"result": True})
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(data.encode())
        else:
            self.send_error(404)

    def do_POST(self):
        parse_uri = urlparse(self.path)
        if parse_uri.path == "/error_notify":
            content_length = int(self.headers.get('Content-Length', 0))
            post_data = self.rfile.read(content_length)
            print("Received notification:", post_data.decode())
            self.send_response(200)
            self.end_headers()
        else:
            self.send_error(404)

if __name__ == "__main__":
    if len(sys.argv) < 3:
        print("Usage: python server.py <host> <port>")
    else:
        host = sys.argv[1]
        port = int(sys.argv[2])
        print(f"Starting server on {host}:{port}")
        server = HTTPServer((host, port), LoginHandler)
        server.serve_forever()